package io.jsen.meta.diff.watch;

import java.util.*;

/**
 * @author jsen
 * @date 2021/04/30
 **/
public class MetaPath implements Iterable<String> {
    List<String> pathList;
    boolean fromRoot;

    MetaPath(List<String> pathList, boolean fromRoot) {
        this.pathList = pathList;
        this.fromRoot = fromRoot;
    }

    @Override
    @SuppressWarnings("all")
    public Iterator<String> iterator() {
        return pathList.iterator();
    }

    public static class Builder extends ArrayList<String> {
        /**
         * 是不是从metaObject的root开始查询
         */
        private final boolean fromRoot;

        private Builder(boolean fromRoot) {
            add(null);
            this.fromRoot = fromRoot;
        }

        public Builder p(String path) {
            addAll(Arrays.asList(path.split("\\.")));
            return this;
        }


        public MetaPath build() {
            return new MetaPath(Collections.unmodifiableList(this), fromRoot);
        }
    }


    public static Builder builder(boolean fromRoot) {
        return new Builder(fromRoot);
    }
}
